package com.rao.gmall.pay.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rao.gmall.pay.config.AppAlipayConfig;
import com.rao.gmall.pay.service.PayService;
import com.rao.gmall.pay.vo.PayParamsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PayServiceImpl implements PayService {


    @Autowired
    AlipayClient alipayClient;

    @Autowired
    AppAlipayConfig alipayConfig;




    /**
     * vo收集到页面支付请求的核心信息
     * @param vo
     * @return
     */
    public String payOrder(PayParamsVo vo) throws Exception {

        //1、创建一个支付请求
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayConfig.getReturn_url());
        alipayRequest.setNotifyUrl(alipayConfig.getNotify_url());


        //2、设置支付请求的请求体详情
        String json = new ObjectMapper().writeValueAsString(vo);
        alipayRequest.setBizContent(json);

        //3、调用支付宝的支付功能
        String result = alipayClient.pageExecute(alipayRequest).getBody();


        //result //代表的是支付宝返回的二维码支付页


        //返回支付宝的二维码支付页
        return result;
    }
}
